using System;
using System.Collections.Generic;

namespace LinqRepositorySample.Domain
{
    public class Order
    {
        #region fields
        private int _orderId;
        private Customer _customer;
        private bool _delivered;
        private IList<OrderDetail> _orderDetails = new List<OrderDetail>();
        #endregion

        #region properties
        public virtual int OrderId
        {
            get { return _orderId; }
        }

        public virtual Customer Customer
        {
            get { return _customer; }
            set
            {
                if (_customer != null)
                {
                    _customer.Orders.Remove(this);
                    CustomerId = 0;
                }
                _customer = value;
                if (_customer != null)
                {
                    _customer.Orders.Add(this);
                    CustomerId = _customer.CustomerId;
                }
            }
        }

        public virtual int CustomerId
        {
            get;
            set;
        }

        public virtual DateTime OrderDate { get; set; }

        public virtual DateTime ScheduledDelivery { get; set; }

        public virtual bool Delivered { get; set; }
        
        public virtual IList<OrderDetail> OrderDetails
        {
            get { return _orderDetails; }
        }
        #endregion

        #region methods
        public virtual void AddOrderDetail (OrderDetail detail)
        {
            if (detail == null)
                throw new ArgumentNullException("detail");
            _orderDetails.Add(detail);
            detail.Order = this;
        }

        public virtual void RemoveOrderDetail (OrderDetail detail)
        {
            if (detail == null)
                throw new ArgumentNullException("detail");
            _orderDetails.Remove(detail);
            detail.Order = null;
        }
        #endregion
    }
}